/**********************
**intellistrip.h
**Date Created: 4/18/2009
**Copyright 2009 Tomorrow Tech Industries
**Contact: RPITomorrowTech@gmail.com
**********************/

#ifndef INTELLISTRIP_H
#define INTELLISTRIP_H

/*
Protocol
Master slave architecture
Host computer sends serial requests
Intellistrip sends serial responses
All requests must have an intellistrip serial header object
*/

#ifdef __cplusplus
extern "C"
{
#endif

/**********
Sizes
These are really important since we are doing serial communications
**********/

//GCC
#ifdef __GNUC__
typedef unsigned char				ttUI8_t;
typedef signed char					ttI8_t;
typedef unsigned short int			ttUI16_t;
typedef signed short int			ttI16_t;
typedef unsigned int				ttUI32_t;
typedef signed int					ttI32_t;
typedef unsigned long				ttUI64_t;
typedef signed long					ttI64_t;

//8051
#else

typedef unsigned char				ttUI8_t;
typedef signed char					ttI8_t;
typedef unsigned int				ttUI16_t;
typedef signed int					ttI16_t;

#endif

//Let comfterable size for architecture be this
typedef int							ttRC;

/****************************************
Success codes
****************************************/

#define TT_OK				1

//Succeeded, but something was suspicious
#define TT_WARNING			2

/****************************************
Failure codes
****************************************/
//Generic error
#define TT_GENERAL			-1

//Out of memory of other memory error
#define TT_MEM				-2

//Access denied or other permissions issues
#define TT_ACCESS			-3

//A network resource is unavailable
#define TT_NETWORK			-4
	
//An invalid input was detected
#define TT_INVALID_INPUT	-5

//Unsupported Feature
#define TT_UNSUPPORTED		-6

//Return true if succeeded, false otherwise
#define TT_SUCCEEDED(x)		(x >= 0)	

//Return true if failed, false otherwise
#define TT_FAILED(x)		(x < 0)


/*************
Requests to Intellistrip
*************/

//Don't actually send this
#define REQUEST__INVALID				0

//getPortPower
#define REQUEST__GET_PORT_POWER			1
//"addtional" data
//The port
#define REQUEST__GET_PORT_POWER__PORT	0
//Power level response
#define RESPONSE__GET_PORT_POWER__LEVEL	0

//setPortPower
#define REQUEST__SET_PORT_POWER			2
//"addtional" data
//The port
#define REQUEST__SET_PORT_POWER__PORT	0
//Power level, from 0 - 100
#define REQUEST__SET_PORT_POWER__LEVEL	1

//This will be emulated on host for now
#define REQUEST__GET_INFORMATION		3


//Maximum total request size in bytes we will accept
#define MAX_REQUEST__PAYLOAD			16
#define MAX_REQUEST__SIZE			(sizeof(struct IntellistripSerialRequest) + MAX_REQUEST__PAYLOAD)

//A data checksum to help make sure we aren't recieving "wrong" data
//ie from the JTAG adapter
//Checksum is short, so we still have a moderate chace of collisions
//So don't depend on it
#define intellistripSerialHeaderCalculateChecksum(dataIn, dataSize) 0x84
#if 0
ttUI8_t intellistripSerialHeaderCalculateChecksum(char *dataIn, ttUI8_t dataSize)
{
	ttUI8_t ret = 0x84;
	
	if( dataSize == 0 )
	{
		return ret;
	}
	for( ;; )
	{
		//Simple add checksum
		ret += *dataIn;

		//Don't bother with the last decrement
		if( dataSize == 1 )
		{
			return ret;
		}
		//Advance our position and decrease remaining data
		--dataSize;
		++dataIn;
	}
}
#endif

struct IntellistripSerialHeader
{
	//Size of they payload (ie not including the header)
	ttUI8_t size;
	//Checksum to make sure we are getting reasonable data
	ttUI8_t checksum;
};

struct IntellistripSerialRequest
{
	//Serial data header
	struct IntellistripSerialHeader header;

	//The request
	ttUI8_t request;
	//Additional parameters, request specific
	//May be unused
	//Size is calculated from header size
	ttUI8_t additional[0];
};

struct IntellistripSerialResponse
{
	//Serial data header
	struct IntellistripSerialHeader header;
	//TT_* return code
	ttI8_t rc;

	//Generic response data, request specific
	//Size is calculated from header size
	ttUI8_t additional[0];
};



#ifdef __cplusplus
}
#endif

#endif
